export const useComfy = () => {
  const { isStart } = storeToRefs(comfyStore());
  const isStartHandler = async () => {
    const { response } = await useTauriRequest("http://127.0.0.1:8188", null, {
      responseType: "TEXT",
      method: "GET",
    });
    if (response.status === 200) {
      isStart.value = true;
    } else {
      isStart.value = false;
    }
    return isStart.value;
  };
  isStartHandler();
  const loading = ref(false);
  const start = async () => {
    loading.value = true;
    if (await isStartHandler()) return;
    const interval = setInterval(async () => {
      const response = await isStartHandler();
      if (response) {
        clearInterval(interval);
        loading.value = false;
      }
    }, 500);
    await Tauri.comfyStart();
  };
  const openWebView = () => {
    Tauri.createWindow({
      label: "comfyUI",
      width: 1000,
      height: 600,
      title: "comfyUI",
      url: "/#/webview/comfyUI",
    });
    Tauri.showWindow("comfyUI");
  };
  return { start, loading, isStart, openWebView };
};
